<?php
class NodesController extends AppController {

	var $name = 'Nodes';
	//var $helpers = array('Html', 'Form');
	var $helpers = array('Html', 'Form', 'Extend.Textile','Images.Embed');
	var $paginate = array(
		'limit' => 30,
		'order' => array('Node.lft' => 'asc'),
		'conditions' => array('Node.published' => 1)
	);
	
	function beforeFilter() {
		parent::beforeFilter(); 
		//$children = $this->Node->fetch(null, 'children');
		//$this->set('root', $this->Node->fetch(null));
		//$this->set('menu', $this->Node->fetch(null, 'children'));
	}
	
	/*$allChildren = $this->Category->children(1);
	$directChildren = $this->Category->children(1, true);
	$parent = $this->Category->getparentnode(2);
	$parents = $this->Category->getpath(15);
	*/
	
	/* FRONTEND FUNCTIONS */
	
	function fetch($id = null, $type = 'node') {
		return $this->Node->fetch($id, $type);
	}
	
	function index() {
		//$children = $this->Node->fetch(null, 'children');
		$path = func_get_args();
		
		$path = join('/', $path);
		//debug($path);
		if($path == null) {
			$node = $this->Node->findByParentId(null);
		} else {
			$node = $this->Node->findByPath($path);
		}
		
		if($node) {
			if($this->Node->childcount($node['Node']['id'], true)) {
				$children = $this->Node->children($node['Node']['id'], true);
			}
			
			if($node['Node']['depth']>1) {
				$path = $this->Node->getPath($node['Node']['id'], 'id');
				$children = $this->Node->children($path[0]['Node']['id'], true);
				//debug($path);
			}
			if(isset($children)) {
				$this->set('children', $children);
			}
			//debug($node);
			$this->set('node', $node);
			$this->pageTitle = $node['Node']['title'];
			$there = h(Router::url($node['Node']['shortcut']));
			//debug($there);
			if(!empty($node['Node']['shortcut']) and $this->here != $there) {
				$this->redirect($node['Node']['shortcut']);
			}
			//$this->set('children', $this->Node->fetch($node['Node']['id'], 'children'));
		} else {
			$this->set('name', __('Node not found.', true));
			$this->set('message', $path);
			//$this->Session->setFlash(__('Could not find a node with that path.', true));
			$this->render("../errors/error404");
		}
		
		// Spesify a override template in app_controller to use a spesial view
		
			if(empty($node['Node']['template'])) {
				$template = Configure::read('Nodes.default_template');
				if(!empty($template)) {
					$this->render($template);
				}
			} else {
				$this->render($node['Node']['template']);
			}
			

	}
	
	/* BACKEND FUNCTIONS */
	
	function _getNodeTitles() {
		$nodes = $this->Node->generatetreelist(null, null, null, '--');
		//debug($nodes);
		$this->set('nodes', $nodes);
	}
	
	function admin_sync() {

		$this->Node->sync();
		$this->Session->setFlash(__('Node was synced.', true));
		$this->admin_redirect_index();
	}
	
	function admin_up($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Node.', true));
			$this->admin_redirect_index();
		}
		$this->Node->moveUp(null, $id , 1);
		$this->admin_redirect_index();
	}
	
	function admin_down($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Node.', true));
			$this->admin_redirect_index();
		}
		$this->Node->moveDown(null, $id , 1);
		$this->admin_redirect_index();
	}
	
	function _saveNode() {
		
		if(!empty($this->data)) {
			
			if(empty($this->data['Node']['parent_id'])) {
				$this->data['Node']['parent_id'] = 0;
			}
			//debug($this->data);
			if($this->Node->save($this->data)){
				$this->Node->sync();
				$this->Session->setFlash(__('Node saved', true));
				$this->admin_redirect_index();
			}else{
				$this->Session->setFlash(__('Node not saved', true));
				
			}
		}
		
	}
	
	function _setTemplates() {
		App::import('Core', 'Folder');
		$templates = array();
    	$template_folder = new Folder(VIEWS . "nodes");
		$folder = $template_folder->ls(null, true);
		foreach($folder[1] as $item) {
			$item = str_replace(".ctp", "", $item);
			$templates[$item] = $item . " template";
		}
		$this->set('templates', $templates);
	}
	
	function admin_edit($id = null) {
		$this->_setTemplates();
		$this->_saveNode();
		
		//debug($templates);
		if($id) {
			$node = $this->Node->findById($id);
			$this->data = $node;
			//debug($this->data);
			// Get nodetitles for parent_id
			$this->_getNodeTitles();
		}else{
			$this->admin_redirect_index();
		}
	}

	function admin_add($parent_id = null) {
		$this->_setTemplates();
		$this->set('parent_id', $parent_id);

		// Get nodetitles for parent_id
		$this->_getNodeTitles();
		if($this->data) {
			//$this->data['Node']['parent_id'] = null;
			$this->Node->create();
			$this->_saveNode();
			
		}
		
	}
	
	function admin_index() {
		//$this->Node->recover(null, 'parent', null);
		$this->paginate['conditions'] = null;
		$this->Node->recursive = 0;
		$nodes = $this->paginate();
		$this->set('nodes',$nodes );
	}
	
	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Node.', true));
			$this->admin_redirect_index();
		}
		$this->set('node', $this->Node->read(null, $id));
	}
	
	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Node', true));
			$this->admin_redirect_index();
		}
		if ($this->Node->del($id)) {
			$this->Session->setFlash(__('Node deleted', true));
			$this->admin_redirect_index();
		} else {
			$this->Session->setFlash(__('Node soft deleted', true));
			$this->admin_redirect_index();
		}
	}
	
	function admin_redirect_index() {
		$this->redirect('/admin/nodes/');
	}

}
?>